package com.huarun.redis;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * @author hqy
 * @创建时间   2018-10-12 15:21
 * @Description redis工具
 */
@Component
public class RedisUtils {
	private Logger logger = LoggerFactory.getLogger(RedisUtils.class);
	//操作字符串的template，StringRedisTemplate是RedisTemplate的一个子集
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	//RedisTemplate可以进行所有的操作
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	public String set(RedisKeyPrefix keyPrefix, String key, String value) {
		String cacheKey = getCacheKey(keyPrefix, key);
		ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
		boolean bExistent = this.stringRedisTemplate.hasKey(cacheKey);
		if (bExistent) {
			ops.set(cacheKey, value, 7, TimeUnit.DAYS);
			logger.info("this key is bExistent!");
		} else {
			ops.set(cacheKey, value);

		}
		return key;
	}

	/**
	 * 写入缓存设置时效时间
	 */
	public boolean set(final String key, String value, Long expireTime) {
		try {
			ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
			ops.set(key, value, expireTime, TimeUnit.SECONDS);
			return true;
		} catch (Exception e) {
			logger.error("error",e);
			return false;
		}
	}

	public String get(RedisKeyPrefix keyPrefix, String key) {
		String cacheKey = getCacheKey(keyPrefix, key);
		return this.stringRedisTemplate.opsForValue().get(cacheKey);
	}

	public String setHuarunVal(String key, String value, long time) {
		String cacheKey = getCacheKey(RedisKeyPrefix.HUARUN, key);
		ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
		ops.set(cacheKey, value, time);
		logger.info("this key is bExistent!");
		return key;
	}

	public String getHuarunVal(String key) {
		String cacheKey = getCacheKey(RedisKeyPrefix.HUARUN, key);
		return this.stringRedisTemplate.opsForValue().get(cacheKey);
	}

	public void delHuarunVal(String key) {
		String cacheKey = getCacheKey(RedisKeyPrefix.HUARUN, key);
		this.stringRedisTemplate.delete(cacheKey);
	}

	public void del(RedisKeyPrefix keyPrefix, String key) {
		String cacheKey = getCacheKey(keyPrefix, key);
		this.stringRedisTemplate.delete(cacheKey);
	}

	public void setObj(String key, Object object) {
		redisTemplate.opsForValue().set(key, object);
	}

	public String getObj(String key) {
//		return String.valueOf(redisTemplate.opsForValue().get(key));
		return this.stringRedisTemplate.opsForValue().get(key);
	}

	/**
	 * 功能说明：拼接cachekey
	 * @param keyPrefix key前缀
	 * @param key  key后缀
	 * @return
	 */
	public String getCacheKey(RedisKeyPrefix keyPrefix, String key) {
		return keyPrefix.getKey() + "_" + key;
	}

}
